/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include "CMatrix3x3.h"

#include <math.h>

//constructors
CMatrix3x3::CMatrix3x3()
{
    for (unsigned short i = 0; i < 9; ++i)
        mData[i] = 0.0;

}

CMatrix3x3::CMatrix3x3(float* raw_data)
{
    if (raw_data == NULL)
        return;

    for (unsigned short i = 0; i < 9; ++i)
    {
        mData[i] = raw_data[i];
    }
}

CMatrix3x3::CMatrix3x3(const CMatrix3x3 &source)
{
    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            mData[i*3 + j] = source.mData[i*3 + j];
        }
    }
}

CMatrix3x3::~CMatrix3x3()
{

}

//operators overloading
float& CMatrix3x3::operator() (const unsigned short i, const unsigned short j)
{
    if ( (i < 3) && (j < 3) )
        return mData[i*3 + j];
    else
    {
        std::cout << "Error when calling CMatrix3x3 operator(), requested indexes are out of bounds." << std::endl;
        return mData[0];
    }
}

CMatrix3x3 CMatrix3x3::operator+ (CMatrix3x3 matrix)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[i*3 + j] + matrix(i,j);
        }
    }

    return out;
}

CMatrix3x3 CMatrix3x3::operator+ (const float scalar)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[i*3 + j] + scalar;
        }
    }

    return out;
}

CMatrix3x3 CMatrix3x3::operator- (CMatrix3x3 matrix)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[i*3 + j] - matrix(i,j);
        }
    }

    return out;
}

CMatrix3x3 CMatrix3x3::operator- (const float scalar)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[i*3 + j] - scalar;
        }
    }

    return out;
}

CMatrix3x3 CMatrix3x3::operator* (CMatrix3x3 matrix)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            float tempValue = 0.0;
            unsigned short k = 0;
            while (k < 3)
            {
                tempValue += mData[i*3 + k]*matrix(k,j);
                k++;
            }

            out(i,j) = tempValue;
        }
    }

    return out;
}

CMatrix3x3 CMatrix3x3::operator* (const float scalar)
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[i*3 + j]*scalar;
        }
    }

    return out;
}

CVector3 CMatrix3x3::operator* (CVector3 vector)
{
	CVector3 out;

	out.SetComponents( mData[0] * vector.x() + mData[1] * vector.y() + mData[2] * vector.z(),
			mData[3] * vector.x() + mData[4] * vector.y() + mData[5] * vector.z(),
			mData[6] * vector.x() + mData[7] * vector.y() + mData[8] * vector.z() );

	return out;
}

CMatrix3x3& CMatrix3x3::operator= (CMatrix3x3 matrix)
{
    //if the same instance as "this" is passed as argument, return this without performing
    //any operation
    if (this == &matrix)
        return *this;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            mData[i*3 + j] = matrix(i,j);
        }
    }

    return *this;

}

bool CMatrix3x3::operator== (CMatrix3x3 matrix)
{
     bool equal = true, breakOuterFor = false;

     for (unsigned short i = 0; i < 3; ++i)
     {
         for (unsigned short j = 0; j < 3; ++j)
         {
             if (mData[i*3 + j] != matrix(i,j))
             {
                 equal = false;
                 breakOuterFor = true;
                 break;
             }

             if (breakOuterFor)
                 break;
         }
     }

     return equal;
}

//other functions
void CMatrix3x3::LoadIdentity()
{
	 for (unsigned short i = 0; i < 9; ++i)
	 {
        mData[i] = 0.0;

		if ( (i == 0) || (i == 4) || (i == 8) )
			mData[i] = 1.0;
	 }
}


CMatrix3x3 CMatrix3x3::Transpose()
{
    CMatrix3x3 out;

    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out(i,j) = mData[j*3 + i];
        }
    }

    return out;
}


CVector3 CMatrix3x3::GetRow(const unsigned short i)
{
	CVector3 out(mData[i*3], mData[i*3 + 1], mData[i*3 + 2]);

    return out;
}

CVector3 CMatrix3x3::GetColumn(const unsigned short j)
{
	CVector3 out(mData[j], mData[3 + j], mData[6 + j]);

    return out;
}

float* CMatrix3x3::Data()
{
    return mData;
}

std::ostream & operator << (std::ostream &out, CMatrix3x3 &mat)
{
    for (unsigned short i = 0; i < 3; ++i)
    {
        for (unsigned short j = 0; j < 3; ++j)
        {
            out << mat(i,j) << '\t';
        }
        out << std::endl;
    }
    
    return out;
        
}


